package scau.erps.service.impl;import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.entity.OrderSet;
import scau.erps.service.OrderSetService;

/**
 * 订单参数实现类
 * @author outsider 
 * 
 */
public class OrderSetServiceImpl extends IdEntityService<OrderSet> implements OrderSetService {

	public List<OrderSet> getAllOrderSet() {
		return this.query(null, null);

	}

	@Override
	public OrderSet getOrderSetById(int id) {
		return this.fetch(id);
		
	}

	@Override
	public void updateOrderSet(OrderSet orderSet) {
		this.dao().update(orderSet);
		
	}

	@Override
	public int getTotalByProductAndYear(String product, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(product != null){
			e1 = Cnd.exps("product", "=", product);
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", year);
		}
		List<OrderSet> list =  this.query(Cnd.where(e1).and(e2), null);
		int sum = 0;
		for (OrderSet orderSet : list) {
			sum = sum + orderSet.getTotal();
		}
		return sum;
	}

	@Override
	public int getTotalByMarketAndYear(String market, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(market != null){
			e1 = Cnd.exps("market", "=", market);
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", year);
		}
		List<OrderSet> list =  this.query(Cnd.where(e1).and(e2), null);
		int sum = 0;
		for (OrderSet orderSet : list) {
			sum = sum + orderSet.getTotal();
		}
		return sum;
	}

	@Override
	public double getAveragePriceByProductAndYear(String product, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(product != null){
			e1 = Cnd.exps("product", "=", product);
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", year);
		}
		List<OrderSet> list =  this.query(Cnd.where(e1).and(e2), null);
		double price = 0;
		int j = 0;
		for (OrderSet orderSet : list) {
			if(orderSet.getPrice()>0){
				price = price + orderSet.getPrice();
				j = j+1;
			}			
		}
		if(j>0){
		return price/j;
		}else{
			return 0;
		}		
	}

	@Override
	public double getScaleByMarketAndYear(String market, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(market != null){
			e1 = Cnd.exps("market", "=", market);
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", year);
		}
		List<OrderSet> list =  this.query(Cnd.where(e1).and(e2), null);
		double sum = 0;
		for (OrderSet orderSet : list) {
			sum = sum + orderSet.getTotal()*orderSet.getPrice();
		}
		return sum;
	}	
}
